<template>
  <div class="app-container">
    <!-- 查询 -->
    <div class="filter-container">
      <el-row :gutter="0">
        <el-col :xs="24" :sm="22">
          <el-input v-model="query.name" class="filter-item" style="width: 200px;" placeholder="名称" clearable />
          <el-input v-model="query.url" class="filter-item" style="width: 200px;" placeholder="URL" clearable />
          <el-input v-model="query.permission_code" class="filter-item" style="width: 200px;" placeholder="权限代码" clearable />
          <el-input v-model="query.parent_id" class="filter-item" style="width: 200px;" placeholder="父ID" clearable />
          <el-button class="filter-item" type="primary" @click="search()">查询</el-button>
          <el-button class="filter-item" @click="refresh()">刷新</el-button>
        </el-col>
        <el-col :xs="24" :sm="2" style="text-align:right;">
          <el-button class="filter-item" type="primary" @click="add()">添加</el-button>
        </el-col>
      </el-row>
    </div>
    <!-- 列表 -->
    <el-table v-loading="loading" :data="datas" :height="height" style="width: 100%" border>
      <el-table-column prop="id" label="ID" min-width="50" sortable="custom" fixed="left" />
      <el-table-column prop="name" label="名称" min-width="60" show-overflow-tooltip />
      <el-table-column prop="url" label="URL" min-width="150" show-overflow-tooltip />
      <el-table-column prop="permission_code" label="权限代码" min-width="60" show-overflow-tooltip />
      <el-table-column prop="sort" label="排序" min-width="30" show-overflow-tooltip />
      <el-table-column prop="remark" label="备注" min-width="30" show-overflow-tooltip />
      <el-table-column label="操作" min-width="100" align="right" fixed="right">
        <template slot-scope="{ row }">
          <el-button size="mini" type="success" @click="edit(row)">修改</el-button>
          <el-button size="mini" type="danger" @click="del(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination v-show="count > 0" :total="count" :page.sync="query.page" :limit.sync="query.limit" @pagination="list" />
    <!-- 添加、修改 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialog" top="1vh" :before-close="cancel">
      <el-form ref="ref" :rules="rules" :model="model" label-width="100px" class="dialog-body" :style="{height:height+30+'px'}">
        <el-form-item label="名称" prop="name">
          <el-input v-model="model.name" placeholder="请输入名称" clearable />
        </el-form-item>
        <el-form-item label="URL" prop="url">
          <el-input v-model="model.url" placeholder="请输入URL" clearable />
        </el-form-item>
        <el-form-item label="权限代码" prop="permission_code">
          <el-input v-model="model.permission_code" placeholder="请输入权限代码" clearable />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="model.sort" placeholder="请输入排序" clearable />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="model.remark" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="submit">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import screenHeight from '@/utils/screen-height'
import Pagination from '@/components/Pagination'
import { list, info, add, edit, del } from '@/api/module'

export default {
  name: 'Role',
  components: { Pagination },
  data() {
    return {
      height: 680,
      loading: false,
      datas: [],
      count: 0,
      query: {
        page: 1,
        limit: 13
      },
      dialog: false,
      dialogTitle: '',
      model: {
        parent_id: '',
        id: '',
        name: '',
        url: '',
        permission_code:'',
        sort: 200,
        remark: ''
      },
      rules: {
        name: [{ required: true, message: '请输入模块名称', trigger: 'blur' }],
      },
      menuTree: [],
      userDialog: false,
      userDialogTitle: '',
      userLoad: false,
      userData: [],
      userCount: 0,
      userQuery: {}
    }
  },
  created() {
    this.height = screenHeight()
    this.list()
  },
  methods: {
    // 列表
    list() {
      this.loading = true
      list(this.query).then(res => {
        this.datas = res.data.list
        this.count = res.data.count
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    // 添加
    add() {
      this.dialog = true
      this.dialogTitle = '添加'
      // menuTree().then(res => {
      //   this.menuTree = res.data.list
      // })
      this.reset()
    },
    // 修改
    edit(row) {
      this.dialog = true
      this.dialogTitle = '修改：' + row.id
      // menuTree().then(res => {
      //   this.menuTree = res.data.list
      // })
      info({
        id: row.id
      }).then(res => {
        this.reset(res.data)
      })
    },
    // 删除
    del(row) {
      this.$confirm(
        '确定要删除 <span style="color:red">' + row.name + ' </span>吗？',
        '删除：' + row.id,
        { type: 'warning', dangerouslyUseHTMLString: true }
      ).then(() => {
        this.loading = true
        del({
          id: row.id
        }).then(res => {
          this.reset()
          this.list()
          this.$message.success(res.msg)
        }).catch(() => {
          this.loading = false
        })
      }).catch(() => {})
    },
    // 取消
    cancel() {
      this.dialog = false
      this.reset()
    },
    // 提交
    submit() {
      this.$refs['ref'].validate(valid => {
        if (valid) {
          this.loading = true
          console.log('okkkkkkkkkkkkkkkk')
          console.log(this.model.id)
          if (this.model.id) {
            edit(this.model).then(res => {
              this.list()
              this.reset()
              this.dialog = false
              this.$message.success(res.msg)
            }).catch(() => {
              this.loading = false
            })
          } else {
            add(this.model).then(res => {
              this.list()
              this.reset()
              this.dialog = false
              this.$message.success(res.msg)
            }).catch(() => {
              this.loading = false
            })
          }
        }
      })
    },
    // 重置
    reset(row) {
      if (row) {
        this.model = row
      } else {
        this.model = this.$options.data().model
      }
      if (this.$refs['ref'] !== undefined) {
        this.$refs['ref'].resetFields()
      }
    },
    // 查询
    search() {
      this.query.page = 1
      this.list()
    },
    // 刷新
    refresh() {
      this.query = this.$options.data().query
      this.list()
    }
  }
}
</script>

<style scoped>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
</style>
